<template>
  <div class="infomation">
    <!-- 顶部 -->
    <div class="infoTop">
      <div class="topLab">
        <span class="act">个人信息</span>
      </div>
    </div>
    <!-- 用户头像 -->
    <div class="myinfo">
      <div class="avatar">
        <img src="@/assets/common/img.jpeg" alt="">
      </div>
      <!-- 用户个人信息 -->
      <el-form ref="form" :model="formData" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="formData.username" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="formData.mobile" />
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="formData.sex" placeholder="请选择性别">
            <el-option label="男" value="男" />
            <el-option label="女" value="女" />
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期">
          <el-col>
            <el-date-picker
              v-model="formData.dateOfBirth"
              type="date"
              placeholder="选择日期"
            />
          </el-col>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item>
          <el-button type="primary" @click="onSubmit">保存</el-button>
          <el-button @click="onCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { updateUser } from '@/api/user'
import { getPersonalDetail, updatePersonal } from '@/api/employees'
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      formData: {
        username: '',
        mobile: '',
        sex: '',
        dateOfBirth: ''
      }
    }
  },
  computed: {
    ...mapGetters(['userId'])
  },
  created() {
    this.getPersonalDetail()
  },
  methods: {
    //   获取用户个人信息
    async getPersonalDetail() {
      this.formData = await getPersonalDetail(this.userId)
    },
    //   提交按钮 修改个人信息
    async onSubmit() {
      // 调用api更新个人信息
      await updateUser(this.formData)
      await updatePersonal(this.formData)
      //   页面重新渲染
      this.$store.dispatch('user/getUserInfo')
      //   用户提示
      this.$message.success('修改成功')
    },
    // 取消按钮,回退到首页
    async onCancel() {
      this.$router.go(-1)
    }
  }
}
</script>

<style lang="scss" scoped>
.infomation {
  .infoTop {
    color: #666;
    background: #fff;
    border-bottom: 1px solid #ccc;
    line-height: 40px;
    .topLab {
      // 个人信息样式
      .act {
        color: #409eff;
        border-bottom: 2px solid #409eff;
        width: 120px;
        display: block;
        text-align: center;
        font-size: 18px;
        font-weight: 700;
      }
    }
  }
  //   表单样式
  .myinfo {
    display: flex;
    justify-content: flex-start;
    padding-top: 30px;
    padding: 30px 30px 0;
    .el-select {
      width: 220px;
    }
  }
  //   头像图片样式
  .avatar {
    margin-right: 50px;
    img {
      width: 100px;
      height: 100px;
    }
  }
}
</style>
